import 'package:get/get.dart';
import 'package:ball_lion/entity/expert/expert_top_entity.dart';
import 'package:ball_lion/entity/home/news_class_entity.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/utils/utils.dart';

class LeagueTopListLogic extends GetxController {
  LeagueTopListLogic(this.sportsId);
  final int sportsId;
  List<NewsClassEntity>? condition;
  List<NewsClassEntity>? leagues;
  int conditionIndex = 0;
  int leagueIndex = 0;
  List<ExpertTopEntity>? list;
  List<ExpertTopEntity>? originList;
  bool onlySale = false;

  @override
  void onInit() {
    super.onInit();
    condition = [
      NewsClassEntity(id: 3, name: '连红'),
      NewsClassEntity(id: 1, name: '命中'),
      NewsClassEntity(id: 2, name: '回报')
    ];
  }

  @override
  void onReady() {
    super.onReady();
    getLeague();
  }

  getLeague() async {
    final result = await ExpertApi.getTopLeagues(sportsId);
    if (result != null) {
      leagues = result;
      update();
      getData();
    }
  }

  getData() async {
    List<ExpertTopEntity>? data = await ExpertApi.getLeagueTop(
        condition![conditionIndex].id!, sportsId, leagues![leagueIndex].id!);
    if (data != null) {
      originList = data;
      filterData();
      update();
    }
  }

  filterData() {
    if (!onlySale) {
      list = originList;
    } else {
      list = originList!.filter((item) => item.isSale == 1);
    }
  }

  onTapCondition(int index) {
    if (conditionIndex == index) {
      return;
    }
    conditionIndex = index;
    update();
    getData();
  }

  onTapFilter() async {
    onlySale = !onlySale;
    filterData();
    update();
  }

  onSelectLeague(int index) {
    leagueIndex = index;
    getData();
  }

  onRefresh() {
    getData();
  }
}
